import 'package:dio/dio.dart';
import 'package:wan_android_flutter/repository/datas/search_data.dart';

import '../http/dio_instance.dart';
import 'datas/auth_data.dart';
import 'datas/common_website_data.dart';
import 'datas/home_banner_data.dart';
import 'datas/home_list_data.dart';
import 'datas/knowledge_detail_list_data.dart';
import 'datas/knowledge_list_data.dart';
import 'datas/my_collects_data.dart';
import 'datas/search_hot_keys_data.dart';

class Api {
  static Api instance = Api._();

  Api._();

  ///获取首页Banner数据
  // 异步方法，用于获取Banner数据
  Future<List<HomeBannerData>?> getBanner() async {
    // 发起GET请求，获取Banner数据
    Response response = await DioInstance.instance()
        .get(path: "banner/json"); // 使用await等待异步操作完成

    // 将响应数据解析为HomeBannerData对象
    HomeBannerListData bannerData = HomeBannerListData.fromJson(response.data);
    return bannerData.bannerList;
  }

  ///获取首页文章列表
  Future<List<HomeListItemData>?> getHomeList(String pageCount) async {
    Response response =
        await DioInstance.instance().get(path: "article/list/$pageCount/json");
    HomeListData homeData = HomeListData.fromJson(response.data);
    return homeData.datas;
  }

  ///获取首页置顶数据
  Future<List<HomeListItemData>?> getHomeTopList() async {
    Response response =
        await DioInstance.instance().get(path: "article/top/json");
    HomeTopListData homeData = HomeTopListData.fromJson(response.data);
    return homeData.topList;
  }

  ///获取常用网站
  Future<List<CommonWebsiteData>?> getWebsiteList() async {
    Response response = await DioInstance.instance().get(path: "friend/json");
    CommonWebsiteListData websiteListData =
        CommonWebsiteListData.fromJson(response.data);
    return websiteListData.websiteList;
  }

  ///获取搜索热点
  Future<List<SearchHotKeysData>?> getSearchHotKeys() async {
    Response response = await DioInstance.instance().get(path: "hotkey/json");
    SearchHotKeysListData hotKeysListData =
        SearchHotKeysListData.fromJson(response.data);
    return hotKeysListData.keyList;
  }

  ///注册
  Future<dynamic> register(
      {String? name, String? password, String? rePassword}) async {
    Response response = await DioInstance.instance().post(
        path: "user/register",
        queryParameters: {
          "username": name,
          "password": password,
          "repassword": rePassword
        });
    return response.data;
  }

  ///登录
  Future<AuthData> login({String? name, String? password}) async {
    Response response = await DioInstance.instance().post(
        path: "user/login",
        queryParameters: {"username": name, "password": password});
    return AuthData.fromJson(response.data);
  }

  ///获取体系数据
  Future<List<KnowledgeListData?>> knowledgeList() async {
    Response response = await DioInstance.instance().get(
      path: "tree/json",
    );
    KnowledgeData knowledgeData = KnowledgeData.fromJson(response.data);
    return knowledgeData.list;
  }

  ///收藏站内文章
  Future<bool?> collect(String? id) async {
    Response response =
        await DioInstance.instance().post(path: "lg/collect/$id/json");
    if (response.data != null && response.data is bool) {
      return response.data;
    }
    return false;
  }

  ///取消收藏-站内文章
  Future<bool?> unCollect(String? id) async {
    Response response = await DioInstance.instance()
        .post(path: "lg/uncollect_originId/$id/json");
    if (response.data != null && response.data is bool) {
      return response.data;
    }
    return false;
  }

  ///退出登录
  Future<bool?> logout() async {
    Response response =
        await DioInstance.instance().get(path: "user/logout/json");
    if (response.data != null && response.data is bool) {
      return response.data;
    }
    return false;
  }

  ///知识体系明细数据
  Future<List<KnowledgeDetailItemData>?> detailKnowledgeList(
      String? pageCount, String? cid) async {
    Response response = await DioInstance.instance()
        .get(path: "article/list/$pageCount/json", param: {"cid": cid});
    KnowledgeDetailListData knowledgeDetailListData =
        KnowledgeDetailListData.fromJson(response.data);
    return knowledgeDetailListData.datas;
  }

  ///搜索数据
  Future<List<SearchListData>?> searchList(String? keyword) async {
    Response response = await DioInstance.instance()
        .post(path: "article/query/0/json", queryParameters: {"k": keyword});
    SearchData searchData = SearchData.fromJson(response.data);
    return searchData.datas;
  }

  ///获取我的收藏列表
  Future<List<MyCollectItemData>?> getMyCollects(String pageCount) async {
    Response rsp = await DioInstance.instance()
        .get(path: "lg/collect/list/$pageCount/json");
    MyCollectsData? model = MyCollectsData.fromJson(rsp.data);
    if (model.datas != null && model.datas?.isNotEmpty == true) {
      return model.datas;
    }
    return [];
  }

  ///取消收藏文章
  Future<bool> cancelCollect(String id) async {
    Response response = await DioInstance.instance()
        .post(path: "lg/uncollect_originId/$id/json");
    if (response.data != null && response.data == true) {
      return true;
    }
    return false;
  }
}
